/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadEnderecoEstoque.java
 *
 * Created on 14/10/2011, 19:20:52
 */
package visao;

import EnderecoEstoque.EnderecoEstoque;
import EnderecoEstoque.EnderecoEstoqueJaCadastradoException;
import EnderecoEstoque.EnderecoEstoqueNaoCadastradoException;
import empresa.Empresa;
import fachada.FachadaBDR;
import java.awt.Color;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;

/**
 *
 * @author Administrador
 */
public class CadEnderecoEstoque extends javax.swing.JFrame {

    /** Creates new form CadEnderecoEstoque */
    public CadEnderecoEstoque() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/EndEstoque.gif")).getImage());
    }

    public CadEnderecoEstoque(int idEnderecoEstoque) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/EndEstoque.gif")).getImage());
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // obtendo a categoria
            EnderecoEstoque objEnderecoEstoque = objFachadaBDR.procurarEnderecoEstoque(idEnderecoEstoque);

            id_endereco_estoque.setText(String.valueOf(idEnderecoEstoque));
            jTextArea_Descricao.setText(objEnderecoEstoque.getDescricaoEndereco());
            jTextField_Rua.setText(objEnderecoEstoque.getRuaEstoque());
            jCheckBox_Status.setSelected(objEnderecoEstoque.getStatusEnderecoEstoque());
            jComboBox_Empresa.setSelectedItem(new KeyValue(objEnderecoEstoque.getObjEmpresa().getDescricaoEmpresa(), Integer.toString(objEnderecoEstoque.getObjEmpresa().getIdEmpresa())));

        } catch (EnderecoEstoqueNaoCadastradoException ex) {
            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);
        } catch (CamposObrigatoriosException ex) {
            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);
        } catch (ErroAcessoRepositorioException ex) {
            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Descricao = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_Descricao = new javax.swing.JTextArea();
        jLabel_Rua = new javax.swing.JLabel();
        jTextField_Rua = new javax.swing.JTextField();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jLabel_Menssagem = new javax.swing.JLabel();
        jLabel_Empresa = new javax.swing.JLabel();
        jComboBox_Empresa = new javax.swing.JComboBox();
        id_endereco_estoque = new javax.swing.JTextField();

        setTitle("Endereço Estoque");
        setResizable(false);

        jLabel_Descricao.setText("Descrição :");

        jTextArea_Descricao.setColumns(20);
        jTextArea_Descricao.setRows(5);
        jScrollPane1.setViewportView(jTextArea_Descricao);

        jLabel_Rua.setText("Rua :");

        jButton_Ok.setText("OK");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jLabel_Empresa.setText("Empresa :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<Empresa> listEmpresa = objFachadaBDR.listarEmpresa("");

            jComboBox_Empresa.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (Empresa objEmpresa : listEmpresa) {
                jComboBox_Empresa.addItem(new KeyValue(objEmpresa.getNomeFantasia(), Integer.toString(objEmpresa.getIdEmpresa())));
            }

        } catch (Exception e) {
        }

        id_endereco_estoque.setVisible(false);
        id_endereco_estoque.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jLabel_Menssagem, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel_Rua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_Rua, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel_Empresa)
                                .addComponent(jLabel_Descricao))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGap(32, 32, 32)
                                    .addComponent(id_endereco_estoque, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE)
                                    .addComponent(jCheckBox_Status))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addComponent(jComboBox_Empresa, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_Empresa)
                    .addComponent(jComboBox_Empresa, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Descricao)
                    .addComponent(jCheckBox_Status)
                    .addComponent(id_endereco_estoque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Rua)
                    .addComponent(jTextField_Rua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton_Ok)
                        .addComponent(jButton_Cancelar))
                    .addComponent(jLabel_Menssagem, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idEnderecoEstoque = Integer.parseInt(id_endereco_estoque.getText());
        String descricao = jTextArea_Descricao.getText();
        String rua = jTextField_Rua.getText();
        Boolean status = jCheckBox_Status.isSelected();
        int idEmpresa = Integer.parseInt(((KeyValue) jComboBox_Empresa.getSelectedItem()).getValue());

        try {

            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Empresa objEmpresa = objFachadaBDR.procurarEmpresa(idEmpresa);

            EnderecoEstoque EndEstoque = new EnderecoEstoque(idEnderecoEstoque, objEmpresa, rua, descricao, status, null);

            if (idEnderecoEstoque == 0) {

                objFachadaBDR.cadastarEnderecoEstoque(EndEstoque);

                jTextArea_Descricao.setText("");
                jTextField_Rua.setText("");
                jCheckBox_Status.setSelected(false);
                jComboBox_Empresa.setSelectedIndex(0);

            } else {
                objFachadaBDR.atualizarEnderecoEstoque(EndEstoque);
            }


            String acao = (idEnderecoEstoque == 0) ? "Cadastrada" : "Editada";
            jLabel_Menssagem.setText("Endereço Estoque " + acao + " Com Sucesso!!!");
            jLabel_Menssagem.setForeground(Color.BLACK);



        } catch (EnderecoEstoqueJaCadastradoException ex) {
            Logger.getLogger(CadEnderecoEstoque.class.getName()).log(Level.SEVERE, null, ex);

            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);


        } catch (CamposObrigatoriosException ex) {
            Logger.getLogger(CadEnderecoEstoque.class.getName()).log(Level.SEVERE, null, ex);

            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);


        } catch (ErroAcessoRepositorioException ex) {
            Logger.getLogger(CadEnderecoEstoque.class.getName()).log(Level.SEVERE, null, ex);

            jLabel_Menssagem.setText(ex.getMessage());
            jLabel_Menssagem.setForeground(Color.red);

        }


    }//GEN-LAST:event_jButton_OkActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }

    }//GEN-LAST:event_jButton_CancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadEnderecoEstoque().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField id_endereco_estoque;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JComboBox jComboBox_Empresa;
    private javax.swing.JLabel jLabel_Descricao;
    private javax.swing.JLabel jLabel_Empresa;
    private javax.swing.JLabel jLabel_Menssagem;
    private javax.swing.JLabel jLabel_Rua;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea_Descricao;
    private javax.swing.JTextField jTextField_Rua;
    // End of variables declaration//GEN-END:variables
}
